
//
//  FactoryUI.swift
//  LoveLife
//
//  Created by 杨阳 on 16/9/6.
//  Copyright © 2016年 yangyang. All rights reserved.
//

import UIKit

class FactoryUI: NSObject {
    
    //使用工厂设计模式：使用类方法将基础控件涉及到的常用属性做一归纳总结，方便统一设置和修改
    //UIView
    static func createView(frame:CGRect) ->UIView {
        let view = UIView(frame: frame)
        return view
    }
    
    //UILabel
    static func createLabel(frame:CGRect,text:String?,textColor:UIColor?,font:UIFont?,textAligment:NSTextAlignment?) ->UILabel {
        let label = UILabel(frame: frame)
        label.text = text
        label.textColor = textColor
        label.font = font
        //对齐方式
        if textAligment != nil {
            label.textAlignment = textAligment!
        }
        
        return label
    }
    
    //UIButton
    static func createButton(type:UIButtonType,frame:CGRect,title:String?,titleColor:UIColor?,imageName:String?,backgroundImageName:String?,target: AnyObject,action:Selector) ->UIButton {
        let button = UIButton(type: type)
        //设置frame
        button.frame = frame
        //设置标题
        button.setTitle(title, forState: UIControlState.Normal)
        //设置标题颜色
        button.setTitleColor(titleColor, forState: .Normal)
        //设置图片
        if imageName != nil {
            button.setImage(UIImage(named: imageName!), forState: .Normal)
        }
        
        //设置背景图片
        if backgroundImageName != nil {
            button.setBackgroundImage(UIImage(named: backgroundImageName!), forState: .Normal)
        }
        
        //添加响应事件
        button.addTarget(target, action: action, forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }
    
    //UIImageVIew
    static func createImageView(frame:CGRect,imageName:String?) ->UIImageView {
        let imageView = UIImageView(frame: frame)
        //设置图片
        if imageName != nil {
            imageView.image = UIImage(named: imageName!)
        }
        
        return imageView
    }
    
    //MARK: - UITextField
    static func createTextField(frame:CGRect,text: String?,placeholder:String,leftView:UIView?,rightView:UIView?) ->UITextField {
        let textField = UITextField(frame: frame)
        textField.text = text
        textField.placeholder = placeholder
        
        textField.leftView = leftView
        textField.rightView = rightView
        
        return textField
    }
    
    
    
    
    
    

}
